package com.skyyan.netty.jjf.test;

import cn.hutool.core.util.HexUtil;
import com.skyyan.netty.jjf.protocol.OpenCardMessage;

/**
 * 开卡报文测试主类
 * 包含可独立运行的上行和下行测试方法
 */
public class OpenCardMessageTestMain {

    public static void main(String[] args) {
        try {
            System.out.println("=== 开卡报文完整测试 ===\n");

            // 测试上行报文
            testUplinkMessage();

            // 测试下行报文
            testDownlinkMessage();

            System.out.println("=== 所有测试完成 ===");
        } catch (Exception e) {
            System.err.println("测试过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 测试开卡上行报文（设备→中心）
     * 验证报文的编码和解码功能
     */
    public static void testUplinkMessage() throws Exception {
        System.out.println("=== 开卡上行报文测试 ===");

        // 准备测试数据
        byte[] testAddress = new byte[]{
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
                0x11, 0x12, 0x13
        };

        String orderId = "1234567890123456";
        String name = "张三";
        String phone = "13812345678";
        String idCard = "110101199001011234";
        int areaCode = 1234;
        byte[] cardNo = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05};




        // 创建上行报文
        System.out.println("\n--- 创建上行报文 ---");
        OpenCardMessage.Uplink uplink = new OpenCardMessage.Uplink(
                testAddress,  name, phone, idCard, areaCode, cardNo);

        System.out.println("上行报文创建完成: " + uplink.toString());

        // 获取报文各部分数据
        byte[] uplinkAddress = uplink.getAddress();
        byte uplinkControl = uplink.getControl();
        byte uplinkAfn = uplink.getAfn();
        byte[] uplinkUserData = uplink.getUserData();

        orderId=uplink.getOrderId();
        System.out.println("测试数据:");
        System.out.println("  订单号: " + orderId);
        System.out.println("  姓名: " + name);
        System.out.println("  电话: " + phone);
        System.out.println("  身份证: " + idCard);
        System.out.println("  区域码: " + areaCode);
        System.out.println("  卡号: " + HexUtil.encodeHexStr(cardNo));




        System.out.println("上行报文地址域: " + HexUtil.encodeHexStr(uplinkAddress));
        System.out.println("上行报文控制域: 0x" + Integer.toHexString(uplinkControl & 0xFF));
        System.out.println("上行报文AFN: 0x" + Integer.toHexString(uplinkAfn & 0xFF));
        System.out.println("上行报文用户数据: " + HexUtil.encodeHexStr(uplinkUserData));

        // 解析上行报文
        System.out.println("\n--- 解析上行报文 ---");
        OpenCardMessage.Uplink parsedUplink = new OpenCardMessage.Uplink(uplinkAddress, uplinkUserData);

        System.out.println("解析后的上行报文: " + parsedUplink.toString());
        System.out.println("orderId="+parsedUplink.getOrderId());
        // 验证解析后的数据
        if (!orderId.equals(parsedUplink.getOrderId())) {
            throw new AssertionError("订单号不匹配");
        }

        if (!name.equals(parsedUplink.getName())) {
            throw new AssertionError("姓名不匹配");
        }

        if (!phone.equals(parsedUplink.getPhone())) {
            throw new AssertionError("电话不匹配");
        }

        if (!idCard.equals(parsedUplink.getIdCard())) {
            throw new AssertionError("身份证不匹配");
        }

        if (areaCode != parsedUplink.getAreaCode()) {
            throw new AssertionError("区域码不匹配");
        }

        if (!java.util.Arrays.equals(cardNo, parsedUplink.getCardNo())) {
            throw new AssertionError("卡号不匹配");
        }

        System.out.println("订单号: " + parsedUplink.getOrderId());
        System.out.println("姓名: " + parsedUplink.getName());
        System.out.println("电话: " + parsedUplink.getPhone());
        System.out.println("身份证: " + parsedUplink.getIdCard());
        System.out.println("区域码: " + parsedUplink.getAreaCode());
        System.out.println("卡号: " + HexUtil.encodeHexStr(parsedUplink.getCardNo()));

        System.out.println("=== 开卡上行报文测试完成 ===\n");
    }

    /**
     * 测试开卡下行报文（中心→设备）
     * 验证报文的编码和解码功能
     */
    public static void testDownlinkMessage() throws Exception {
        System.out.println("=== 开卡下行报文测试 ===");

        // 准备测试数据
        byte[] testAddress = new byte[]{
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
                0x11, 0x12, 0x13, 0x14
        };

        String orderId = "1234567890123456";
        byte[] farmerId = new byte[]{
                (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, (byte) 0x55,
                (byte) 0x66, (byte) 0x77, (byte) 0x88, (byte) 0x11, (byte) 0x22,
                (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66, (byte) 0x77,
                (byte) 0x88
        };
        byte status = 0x00; // 成功

        System.out.println("测试数据:");
        System.out.println("  订单号: " + orderId);
        System.out.println("  农户ID: " + HexUtil.encodeHexStr(farmerId));
        System.out.println("  状态码: 0x" + Integer.toHexString(status & 0xFF));

        // 创建下行报文
        System.out.println("\n--- 创建下行报文 ---");
        OpenCardMessage.Downlink downlink = new OpenCardMessage.Downlink(
                testAddress, orderId, farmerId, status);

        System.out.println("下行报文创建完成: " + downlink.toString());

        // 获取报文各部分数据
        byte[] downlinkAddress = downlink.getAddress();
        byte downlinkControl = downlink.getControl();
        byte downlinkAfn = downlink.getAfn();
        byte[] downlinkUserData = downlink.getUserData();

        System.out.println("下行报文地址域: " + HexUtil.encodeHexStr(downlinkAddress));
        System.out.println("下行报文控制域: 0x" + Integer.toHexString(downlinkControl & 0xFF));
        System.out.println("下行报文AFN: 0x" + Integer.toHexString(downlinkAfn & 0xFF));
        System.out.println("下行报文用户数据: " + HexUtil.encodeHexStr(downlinkUserData));

        // 解析下行报文
        System.out.println("\n--- 解析下行报文 ---");
        OpenCardMessage.Downlink parsedDownlink = new OpenCardMessage.Downlink(
                downlinkAddress, downlinkUserData);

        System.out.println("解析后的下行报文: " + parsedDownlink.toString());

        // 验证解析后的数据
        if (!orderId.equals(parsedDownlink.getOrderId())) {
            throw new AssertionError("订单号不匹配");
        }

        if (!java.util.Arrays.equals(farmerId, parsedDownlink.getFarmerId())) {
            throw new AssertionError("农户ID不匹配");
        }

        if (status != parsedDownlink.getStatus()) {
            throw new AssertionError("状态码不匹配");
        }

        if (!parsedDownlink.isSuccess()) {
            throw new AssertionError("应为成功状态");
        }

        if (!"成功".equals(parsedDownlink.getStatusDesc())) {
            throw new AssertionError("状态描述不匹配");
        }

        System.out.println("订单号: " + parsedDownlink.getOrderId());
        System.out.println("农户ID: " + HexUtil.encodeHexStr(parsedDownlink.getFarmerId()));
        System.out.println("状态码: 0x" + Integer.toHexString(parsedDownlink.getStatus() & 0xFF));
        System.out.println("是否成功: " + parsedDownlink.isSuccess());
        System.out.println("状态描述: " + parsedDownlink.getStatusDesc());

        System.out.println("=== 开卡下行报文测试完成 ===\n");
    }
}
